/**
 * 
 */
package database.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import database.DBConnection;

import model.User;

/**
 * @author Triin <3 Matu
 * This class isen't used anymore, instead UserHibernate is used.
 * 
 *
 */
public class UserDAO {
	DBConnection db = new DBConnection();
	public User toUser(ResultSet resultSet){
		int users_count = 0;
		User user = null;
		try {

			// Should actually be only one result
			while (resultSet.next()) {

				users_count = users_count + 1;
				user = new User();
				// user_account on hoopis ID ehk int
				user.setUser(resultSet.getInt("user_account"));
				user.setFirst_name(resultSet.getString("first_name"));
				user.setLast_name(resultSet.getString("last_name"));
				user.setUsername(resultSet.getString("username"));

			}

		}catch (Exception ex) {
			ex.printStackTrace();
			// MyLogger.Log("Auth.UserAuth():",ex.getMessage());
		}
		if (users_count == 1) {
			return user;
		}else{
			if(users_count == 0){
				System.out.println("Wrong username or password");
			}
			return null;
		} 
	}
	public User getUserFromDB(String username, String password){
		String sql = "SELECT E.employee,UA.user_account,UA.username, P.first_name, P.last_name FROM employee E INNER JOIN user_account UA ON E.employee = UA.subject_fk "
				+ "INNER JOIN person P ON E.person_fk = P.person WHERE UA.subject_type_fk = 3  AND UA.username='"
				+ username
				+ "'  AND UA.passw=md5('"
				+ password
				+ "')";
		User user = null;
		try{
			ResultSet resultSet = db.executeQuery(sql);
			user = toUser(resultSet);
		}catch(SQLException e){
			System.out.println("A problem with SQL statement in finding user");
			e.printStackTrace();
		}
		return user;
	}

}
